package com.longrise.android.byjk.plugins.dealsituation.cachecourse.downloadeddetail;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.longrise.android.byjk.R;
import com.longrise.common.base.BaseActivity;
import com.longrise.android.byjk.model.DownloadedDetailBean;
import com.longrise.common.utils.AppUtil;
import com.longrise.android.byjk.utils.DialogUtil;
import com.longrise.filedownloader.DownloadManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by callxkj on 2017/4/6.
 * 下载详情页面
 */

public class DownloadedDetailActivity extends BaseActivity<DownloadedDetailPresenter>
        implements DownloadedDetailContract.View {
    public final static String COURSEID = "COURSEID";
    public static final int OPEN = 0;
    public static final int CLOSE = 1;
    public static final int START = 2;
    public static final int STOP = 3;
    public static final int NORMAL = 4; //正常模式
    public static final int DELETE = 5; //删除模式
    public static final int DOWNLOADSTATUS = 11; //下载状态 单个下载
    public static final int DOWNLOADALLSTATUS = 12; //下载全部
    public static final int STUDYSTATUS = 13; //学习状态
    private int mAllDownloadStatus = STOP;
    private int mStatus = DOWNLOADSTATUS;
    private String mCourseId;
    private Toolbar mToolbar;
    private RecyclerView mRcv;
    private LinearLayout mllbottom;
    private Button mBtcheckall;
    private Button mBtdelete;
    private int mMode = NORMAL;
    private DownloadedDetailRcvAdapter mAdapter;

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_downloadeddetail;
    }

    @Override
    public void initView() {
        getIntentData();
        mToolbar = (Toolbar) findViewById(R.id.byjk_toolbar);
        mToolbar.setTitle(AppUtil.getString(R.string.downloaded_title));
        setSupportActionBar(mToolbar);
        mRcv = (RecyclerView) findViewById(R.id.downloadeddetail_ac_rcv);
        mllbottom = (LinearLayout) findViewById(R.id.downloadeddetail_ac_ll);
        mBtcheckall = (Button) findViewById(R.id.downloadeddetail_ac_check_all);
        mBtdelete = (Button) findViewById(R.id.downloadeddetail_ac_delete);
        initEvent();
        mPresenter.initData(mCourseId);
    }

    private void getIntentData() {
        Intent intent = getIntent();
        mCourseId = intent.getStringExtra(COURSEID);
    }

    @Override
    public void initRcv(DownloadManager downloadManager) {
        List<MultiItemEntity> data = new ArrayList<>();
        mAdapter = new DownloadedDetailRcvAdapter(data,downloadManager,mPresenter,mAllDownloadStatus);
        mRcv.setLayoutManager(new LinearLayoutManager(this));
        mRcv.setAdapter(mAdapter);
    }

    /**
     * 提示在移动网络是否可以下载
     */
    @Override
    public void showDownloadNetTips(final DownloadedDetailBean downloadedDetailBean) {
        View v = View.inflate(mContext, R.layout.dialog_exit, null);
        TextView cacnle_3g = (TextView) v.findViewById(R.id.text_cancle);
        TextView confrim_3g = (TextView) v.findViewById(R.id.text_confirm);
        TextView title = (TextView) v.findViewById(R.id.dialog_hint);
        TextView msg = (TextView) v.findViewById(R.id.text_m);
        title.setText("提示");
        msg.setText("您当前处于非wifi网络环境下，继续使用可能产生流量");
        cacnle_3g.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtil.getInstance().dismiss();
            }
        });
        confrim_3g.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtil.getInstance().dismiss();
                mPresenter.startItem(downloadedDetailBean);
            }
        });
        DialogUtil.getInstance().creatAlertDialog(mContext, v, 266, 159);
    }

    //设置是全部下载或者是全部开始的状态
    @Override
    public void setAllDownloadStatus(int status) {
        mAllDownloadStatus = status;
        mAdapter.setAllDownloadStatus(status);
    }

    private void initEvent() {
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    @Override
    public void showLoading() {
        showLoadingDialog();
    }

    @Override
    public void dismissLoading() {
        dismissLoadingDialog();
    }

    @Override
    public void toast(String msg) {
        showToast(msg);
    }

    //刷新界面
    @Override
    public void updateData(DownloadedTransferBean bean, DownloadManager downloadManager) {
        mAdapter.setData(bean,mMode);
        mAdapter.expand(0);
        mAdapter.notifyDataSetChanged();
    }
}
